<?php

class ControllerCfNews extends Controller {
    
	private $error = array();
 
	public function index() 
    {
		$this->load->language('cf/news');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('cf/news');
		
		$this->getList();
	} 

    // --------------------------------------------------------------------
    
	public function insert() 
    {
		$this->load->language('cf/news');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('cf/news');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_cf_news->addNews($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
						
			$this->redirect(HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url);
		}

		$this->getForm();
	}

    // --------------------------------------------------------------------
    
	public function update() 
    {
		$this->load->language('cf/news');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('cf/news'); 
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_cf_news->editNews($this->request->get['news_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
						
			$this->redirect(HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url);
		}

		$this->getForm();
	}

    // --------------------------------------------------------------------
    
	public function delete() 
    { 
		$this->load->language('cf/news');

		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('cf/news');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $news_id) {
				$this->model_cf_news->deleteNews($news_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
						
			$this->redirect(HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url);
		}

		$this->getList();
	}

    // --------------------------------------------------------------------
    
	private function getList() 
    {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'date_added';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=cf/news/insert&token=' . $this->session->data['token'] . $url;
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=cf/news/delete&token=' . $this->session->data['token'] . $url;	

		$this->data['reviews'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$review_total = $this->model_cf_news->getTotalnews();
	
		$results = $this->model_cf_news->getNews($data);
 
    	foreach ($results as $result) {
			$action = array();
						
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=cf/news/update&token=' . $this->session->data['token'] . '&news_id=' . $result['news_id'] . $url
			);            
						
			$this->data['news'][] = array(
				'news_id'  => $result['news_id'],
				'title'       => $result['title'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'selected'   => isset($this->request->post['selected']) && in_array($result['review_id'], $this->request->post['selected']),
				'action'     => $action
			);
		}
	
        $lang_items_key = 'heading_title|text_no_results|column_title|column_status|column_date_added|column_action|button_insert|button_delete';
        $this->data = array_merge($this->data, $this->language->gets($lang_items_key)); 
        
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_title'] = HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . '&sort=title' . $url;
		$this->data['sort_status'] = HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . '&sort=status' . $url;
		$this->data['sort_date_added'] = HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . '&sort=date_added' . $url;
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'cf/news_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

    // --------------------------------------------------------------------
    
	private function getForm() 
    {
        $lang_items_key = 'heading_title|text_enabled|text_disabled|text_none|text_select|entry_title|entry_description|entry_content|entry_status|entry_text|entry_image|'.
                          'button_save|button_cancel|tab_general|tab_data|text_image_manager|';
        $this->data = array_merge($this->data, $this->language->gets($lang_items_key));

 		$this->data['error_warning'] = isset($this->error['warning']) ? $this->error['warning'] : '';
 		$this->data['error_title']   = isset($this->error['title']) ? $this->error['title'] : '';
        $this->data['error_content'] = isset($this->error['content']) ? $this->error['content'] : '';
        		
		$url = '';
			
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
   		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
        // get languages
        $this->load->model('localisation/language');
        $this->data['languages'] = $this->model_localisation_language->getLanguages();
        								
		if (!isset($this->request->get['news_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=cf/news/insert&token=' . $this->session->data['token'] . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=cf/news/update&token=' . $this->session->data['token'] . '&news_id=' . $this->request->get['news_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=cf/news&token=' . $this->session->data['token'] . $url;

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['news_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$news_info = $this->model_cf_news->getItem($this->request->get['news_id']);
		}
		
        $this->data['status'] = isset($this->request->post['status']) 
                                ? $this->request->post['status'] : ( isset($news_info) ? $news_info['status'] : '' );
        
        $this->data['image'] = isset($this->request->post['image']) 
                               ? $this->request->post['image'] : ( isset($news_info) ? $news_info['image'] : '');

        $this->data['news'] = isset($this->request->post['news']) 
                              ? $this->request->post['news'] : ( isset($news_info) ? $this->model_cf_news->getNewsDetail($this->request->get['news_id']) : array() );
        
        $this->load->model('tool/image');

        if (isset($news_info) && $news_info['image'] && file_exists(DIR_IMAGE . $news_info['image'])) {
            $this->data['preview'] = $this->model_tool_image->resize($news_info['image'], 100, 100);
        } 
        else {
            $this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
        }
        
		$this->template = 'cf/news_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
    // --------------------------------------------------------------------
    
	public function category() 
    {
		$this->load->model('catalog/product');
		
		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}
		
		$product_data = array();
		
		$results = $this->model_catalog_product->getProductsByCategoryId($category_id);
		
		if ($results) {
			foreach ($results as $result) {
				$product_data[] = array(
					'product_id' => $result['product_id'],
					'name'       => $result['name']
				);
			}
		} else {
			$product_data[] = array(
				'product_id' => 0,
				'name'       => $this->language->get('text_none')
			);			
		}
		
		$this->load->library('json');
		
		$this->response->setOutput(Json::encode($product_data));
	}
	
    // --------------------------------------------------------------------
    
	private function validateForm() 
    {        
		if (!$this->user->hasPermission('modify', 'cf/news')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
        
        foreach ($this->request->post['news'] as $language_id => $value) {
            if (DEFAULT_LANG == $language_id && ((strlen(utf8_decode($value['title'])) < 1) || (strlen(utf8_decode($value['title'])) > 200))) {
                $this->error['title'][$language_id] = $this->language->get('error_title');
            }
        }

        return !$this->error ? TRUE : FALSE;
	}

    // --------------------------------------------------------------------
    
	private function validateDelete() 
    {
		if (!$this->user->hasPermission('modify', 'cf/news')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
                                        
		return (!$this->error) ? TRUE : FALSE;
	}
}

?>